home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / delphi.swg / 0218_Direct write to network printer.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-03-04  |  4.1 KB  |  180 lines

  1.  
  2. unit Rawprint;
  3.  
  4. interface
  5. uses printers,windows;
  6.  
  7. type TRawprinter =class(TPrinter)
  8.                   public
  9.                     dc2 : HDC;
  10.                     aborted : boolean;
  11.                     printing : boolean;
  12.                     lasttime : integer;
  13.                     procedure abort;
  14.                     function startraw : boolean;
  15.                     function endraw: boolean;
  16.                     function write(s : string): boolean;
  17.                     function writeln: boolean;
  18.                     destructor destroy; override;
  19.                     procedure settimer;
  20.                     function printerror : boolean;
  21.                   end;
  22. implementation
  23. uses sysutils,forms,dialogs,controls;
  24.  
  25. procedure TRawPrinter.settimer;
  26. begin
  27.   lasttime:=gettickcount;
  28. end;
  29.  
  30. function TRawPrinter.printerror : boolean;
  31. var r : integer;
  32. begin
  33.   result:=false;
  34.   if (gettickcount>lasttime+15000) or (gettickcount<lasttime) then
  35.   begin
  36.     r:=messagedlg('Error '+inttostr(getlasterror)+' Printing on '+printers[printerindex],mterror,[mbretry,mbabort,mbignore],0);
  37.     if r=mrretry then
  38.       result:=false
  39.     else
  40.     begin
  41.       result:=true;
  42.       if r=mrabort then
  43.         abort;
  44.     end;
  45.     settimer;
  46.   end;
  47. end;
  48.  
  49. procedure TRawPrinter.abort;
  50. begin
  51.   abortdoc(dc2);
  52.   endraw;
  53. end;
  54.  
  55. function AbortProc(Prn: HDC; Error: Integer): Bool; stdcall;
  56. begin
  57.   Application.ProcessMessages;
  58.   Result := not TRawprinter(Printer).Aborted;
  59. end;
  60.  
  61. type
  62.   TPrinterDevice = class
  63.     Driver, Device, Port: String;
  64.     constructor Create(ADriver, ADevice, APort: PChar);
  65.     function IsEqual(ADriver, ADevice, APort: PChar): Boolean;
  66.   end;
  67.  
  68. constructor TPrinterDevice.Create(ADriver, ADevice, APort: PChar);
  69. begin
  70.   inherited Create;
  71.   Driver := ADriver;
  72.   Device := ADevice;
  73.   Port := APort;
  74. end;
  75.  
  76. function TPrinterDevice.IsEqual(ADriver, ADevice, APort: PChar): Boolean;
  77. begin
  78.   Result := (Device = ADevice) and (Port = APort);
  79. end;
  80.  
  81.  
  82. destructor TRawprinter.destroy;
  83. begin
  84.   if dc2<>0 then
  85.     deletedc(dc2);
  86. end;
  87.  
  88. function TRawprinter.startraw:boolean;
  89. var
  90.   CTitle: array[0..31] of Char;
  91.   CMode : Array[0..4] of char;
  92.   DocInfo: TDocInfo;
  93.   r : integer;
  94. begin
  95.   result:=false;
  96.   StrPLCopy(CTitle, Title, SizeOf(CTitle) - 1);
  97.   StrPCopy(CMode, 'RAW');
  98.   FillChar(DocInfo, SizeOf(DocInfo), 0);
  99.   with DocInfo do
  100.   begin
  101.     cbSize := SizeOf(DocInfo);
  102.     lpszDocName := CTitle;
  103.     lpszOutput := nil;
  104.     lpszDatatype :=CMode;
  105.   end;
  106.   with TPrinterDevice(Printers.Objects[PrinterIndex]) do
  107.   begin
  108.     if dc2=0 then
  109.     begin
  110.       DC2 := CreateDC(PChar(Driver), PChar(Device), PChar(Port), nil);
  111.       if dc2=0 then
  112.       begin
  113.         result:=false;
  114.         exit;
  115.       end;
  116.      SetAbortProc(dc2, AbortProc);
  117.    end;
  118.   end;
  119.   settimer;
  120.   aborted:=false;
  121.   repeat
  122.     application.processmessages;
  123.   until (StartDoc(dc2, DocInfo)>0) or printerror;
  124.   if not aborted then
  125.     printing:=true;
  126.   result:=printing;
  127. end;
  128.  
  129. function TRawprinter.endraw : boolean;
  130. begin
  131.   settimer;
  132.   if not aborted and printing then
  133.   repeat
  134.     application.processmessages;
  135.   until (windows.enddoc(dc2)>0) or printerror;
  136.   printing:=false;
  137.   result:=not aborted;
  138. end;
  139.  
  140. type passrec = packed record
  141.                  l : word;
  142.                  s : Array[0..255] of char;
  143.                end;
  144. var pass : Passrec;
  145. function TRawprinter.write(s : string):boolean;
  146. var tmp : string;
  147. begin
  148. result:=false;
  149.   if not aborted and printing then
  150.   while s<>'' do
  151.   begin
  152.     result:=false;
  153.     tmp:=copy(s,1,255);
  154.     delete(s,1,255);
  155.     pass.l:=length(tmp);
  156.     strpcopy(pass.s,tmp);
  157.     settimer;
  158.     repeat
  159.       application.processmessages
  160.     until (escape(dc2,PASSTHROUGH,0,@pass,nil)>=0) or printerror;
  161.     if aborted then
  162.       break;
  163.     result:=true;
  164.   end;
  165. end;
  166.  
  167. function TRawprinter.writeln : boolean;
  168. begin
  169.   pass.l:=2;
  170.   strpcopy(pass.s,#13#10);
  171.   settimer;
  172.   repeat
  173.     application.processmessages
  174.   until (escape(dc2,PASSTHROUGH,0,@pass,nil)>=0) or printerror;
  175.   result:=not aborted;
  176. end;
  177.  
  178. end.
  179.  
  180.